﻿using Microsoft.Practices.SharePoint.Common.Logging;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;

namespace SharePoint.Contrib.JobDefinitions
{
    /// <summary>
    /// SPTimerJob to see whether each diagnostic area is already registered as an event source, and registers a new event source if required.
    /// </summary>
    public class EnsureConfiguredAreasRegistered : SPJobDefinition
    {
        private static string name = "ensure-configured-areas-registered";
        private static string title = "EnsureConfiguredAreasRegistered";

        public EnsureConfiguredAreasRegistered()
			: base(name, SPFarm.Local.TimerService, null, SPJobLockType.None)
		{
			base.Title = title;
		}

        [SharePointPermission(System.Security.Permissions.SecurityAction.LinkDemand, ObjectModel = true)]
        public override void Execute(System.Guid targetInstanceId)
        {
            try
            {
                DiagnosticsAreaEventSource.EnsureConfiguredAreasRegistered();
            }
            finally
            {
                this.Delete();
            }
        }

    }
}
